<!DOCTYPE html>
<html>
  
<head>
  <meta charset="utf-8">
  <meta name="author" content="饿包子" />
  
  
  <title>Yarn和npm的命令集和问题 | 饿包子博客</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="Yarn," />
  

  
  <meta name="description" content="昔年我亦赴科场，偶遇仙师古道旁。一阵香风飘羽袖，千条云带绕霓裳。开言句句谈玄理，劝世声声唱洞章。我贵我荣都不羡，重重再教炼黄房。** 吕洞宾 《指玄篇 上篇 其二》 **">

  
  
    <link rel="icon" href="/blog/images/xian.ico">
    <link rel="apple-touch-icon" href="/blog/images/Logo.png">
  

  
<link rel="stylesheet" href="/blog/css/Awesome.min.css">


  
<link rel="stylesheet" href="/blog/css/index.css">
<link rel="stylesheet" href="/blog/styles/components/highlight/highlight.css">


  
  
<script src="/blog/common/jquery.min.js"></script>

  
    
<script src="/blog/common/Av.min.js"></script>

  

  
    
<script src="/blog/common/MathJax.js"></script>

  

  
    
<script src="/blog/common/Valine.min.js"></script>

  

  

  <script>
  // theme-ad's config script
  // it can be used in every script
  
  window.AD_CONFIG = {
    leancloud: {"appid":"gE1EosDsVgB2slV4E92yPkcG-gzGzoHsz","appkey":"niR787UVTMyJQrR6gmOiJCKe","comment":true,"count":true},
    welcome: {"enable":false,"interval":30},
    start_time: "2018-06-10",
    passwords: ["a621ab606db2a11f63edc576a729843b8269250dc324206871d90635ac5e531c", ],
    is_post: true,
    lock: false,
    author: "饿包子",
    share: {"twitter":false,"facebook":false,"weibo":true,"qq":true,"wechat":false},
    mathjax: true,
    page_type: "",
    root: "/blog/"
  };
</script>

  
<script src="/blog/vendor/sha256.min.js"></script>
<script src="/blog/js/auth.js"></script>
<script src="/blog/js/index.js"></script>
<script src="/blog/vendor/qrcode.min.js"></script>


<meta name="generator" content="Hexo 5.4.2"></head>
  <body>
    <header class="site-header">
  <div class="site-header-brand">
    
      <span class="site-header-brand-title">
        <a href="/blog/">清风逐月</a>
      </span>
    
    
      <span class="site-header-brand-motto"> | 我思故我在</span>
    
  </div>
  <div class="site-header-right">
    <nav class="site-header-navigation">
      
        <a href="/blog/" target="_self">主页</a>
      
        <a href="/blog/archives/" target="_self">归档</a>
      
        <a href="/blog/tags/" target="_self">标签</a>
      
        <a href="/blog/categories/" target="_self">分类</a>
      
        <a href="/blog/categories/%E6%97%A5%E8%AE%B0" target="_self">日记</a>
      
    </nav>
    <div class="site-header-btn">
      
        <a href="https://github.com/lixianbin1/" target="_blank" id="site-github">
          <i class="fa fa-github-alt"></i>
        </a>
      
      <a href="javascript:void(0);" id="site-search">
        <i class="fa fa-search"></i>
      </a>
      <a href="javascript:void(0);" id="site-nav-btn">
        <i class="fa fa-ellipsis-v"></i>
      </a>
    </div>
  </div>
</header>
<nav class="table-content" id="site-nav">
  <div class="table-content-title">
    <span>导航</span>
  </div>
  <div class="table-content-main">
    <ol class="toc">
      
        <li class="toc-item">
          <a href="/blog/" target="_self">
            主页
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/archives/" target="_self">
            归档
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/tags/" target="_self">
            标签
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/" target="_self">
            分类
          </a>
        </li>
      
        <li class="toc-item">
          <a href="/blog/categories/%E6%97%A5%E8%AE%B0" target="_self">
            日记
          </a>
        </li>
      
    </ol>
  </div>
</nav>
<div id="site-process"></div>
    <main>
      
  <div class="passage">
  <div class="passage-meta">
    <span>
      <i class="fa fa-calendar"></i>2020-12-10
    </span>
    
    
      <span>
        | <i class="fa fa-unlock-alt"></i>UNLOCK
      </span>
    
    <span>
       | <i class="fa fa-calendar"></i>更新时间:2020-12-10 10:16 
    </span>
  </div>
  <h1 class="passage-title">
    Yarn和npm的命令集和问题
  </h1>
  
  <article class="passage-article">
    <h1 id="yarn"><a href="#yarn" class="headerlink" title="yarn"></a>yarn</h1><p>Yarn 是一个包依赖管理工具，由Facebook提供取代npm的工具</p>
<blockquote>
<p>npm install -g yarn</p>
</blockquote>
<h2 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h2><figure class="highlight md"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">yarn -v     # 获取yarn的版本</span><br><span class="line">yarn init   # 初始化项目</span><br><span class="line">yarn list   # 列举项目包</span><br><span class="line"></span><br><span class="line"><span class="section"># 安装模块包</span></span><br><span class="line">yarn add xxx             // 默认生产依赖 dependencies</span><br><span class="line">yarn add xxx@[version]</span><br><span class="line">yarn add xxx@[tag]</span><br><span class="line">yarn add xxx --dev       // 开发依赖 devDependencies</span><br><span class="line">yarn add xxx --peer      // peerDependencies</span><br><span class="line">yarn add xxx --optional  // optionalDependencies</span><br><span class="line">yarn install  // 安装 yarn.lock 上所有依赖（可以缩略为 yarn ）</span><br><span class="line">yarn global add xxxx     // 全局安装</span><br><span class="line"></span><br><span class="line"><span class="section"># 更新模块包</span></span><br><span class="line">yarn upgrade xxx</span><br><span class="line">yarn upgrade xxx@[version]</span><br><span class="line">yarn upgrade xxx@[tag]</span><br><span class="line"></span><br><span class="line"><span class="section"># 卸载模块包</span></span><br><span class="line">yarn remove xxx</span><br><span class="line"></span><br><span class="line"><span class="section"># 清除缓存</span></span><br><span class="line">yarn cache clean</span><br><span class="line"></span><br><span class="line"><span class="section"># 用户自定义命令</span></span><br><span class="line">yarn xxx      // 类似 npm run xxx</span><br></pre></td></tr></table></figure>
<h1 id="npm"><a href="#npm" class="headerlink" title="npm"></a>npm</h1><p>NPM 是一个包依赖管理工具，NPM是随同NodeJS一起安装的包管理工具，能解决NodeJS代码部署上的很多问题</p>
<h2 id="后缀简介"><a href="#后缀简介" class="headerlink" title="后缀简介"></a>后缀简介</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">-S --save        # 生产阶段的依赖</span><br><span class="line">-D --save-dev    # 开发阶段的依赖</span><br><span class="line">-O --save-optional    # 可选阶段的依赖</span><br><span class="line">-E --save-exact       # 指定版本的依赖</span><br><span class="line">-g               # 全局安装的模块</span><br></pre></td></tr></table></figure>
<h2 id="常用命令-1"><a href="#常用命令-1" class="headerlink" title="常用命令"></a>常用命令</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">npm ls     # 查看安装的模块列表</span><br><span class="line">npm init   # 初始化package文件 -y 缩写可以略去填写</span><br><span class="line">npm help   # 查看命令帮助</span><br><span class="line">npm root   # 查看模块的安装路径</span><br><span class="line">npm config # 管理npm的相关配置</span><br><span class="line">npm cache clean # 清除模块缓存</span><br><span class="line"></span><br><span class="line">npm install xxx       # 安装xxx模块</span><br><span class="line">npm install xxx -g    # 全局安装xxx模块</span><br><span class="line">npm install xxx@1.0.0 # 安装指定1.0.0版本模块</span><br><span class="line"></span><br><span class="line">npm update xxx        # 更新xxx模块</span><br><span class="line">npm uninstall xxx     # 卸载xxx模块</span><br><span class="line">npm outdated          # 列出所有过时模块</span><br></pre></td></tr></table></figure>

<h2 id="相关工具"><a href="#相关工具" class="headerlink" title="相关工具"></a>相关工具</h2><h3 id="镜像源管理"><a href="#镜像源管理" class="headerlink" title="镜像源管理"></a>镜像源管理</h3><p>nrm 是npm的镜像源管理工具，有时候国外资源太慢，使用这个就可以快速地在 npm 源间切换</p>
<blockquote>
<p>npm install nrm -g</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">nrm ls       #查看nrm的镜像源列表和当前源</span><br><span class="line">nrm use xxx  #切换到xxx的印象源</span><br><span class="line"></span><br><span class="line">nrm add xxx http://xxx.cn/ #新增映射源</span><br><span class="line">nrm del xxx  #删除指定的xxx映射源</span><br><span class="line">nrm test npm #测试相应的源的相应时间</span><br></pre></td></tr></table></figure>

<h3 id="包更新管理"><a href="#包更新管理" class="headerlink" title="包更新管理"></a>包更新管理</h3><p>npm-check-updates会将package.json依赖项升级到最新版本，而忽略指定的版本；更新包依赖，所以需要全局安装</p>
<blockquote>
<p>npm install npm-check-updates -g</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">ncu -v  #查看当前ncu版本</span><br><span class="line">ncu     #检测当前项目中可以更新的包依赖</span><br><span class="line">ncu -g  #对全局依赖包进行更新检测</span><br><span class="line"></span><br><span class="line">ncu -u  #检测后可以执行对本地package更新</span><br><span class="line">ncu xxx -f  #更新本地项目指定依赖包</span><br><span class="line"></span><br><span class="line">ncu xxx -x  #筛除本地项目中需要更新的包</span><br><span class="line">ncu &#x27;/^xxx.*$/&#x27;  #使用正则匹配需要更新的包</span><br></pre></td></tr></table></figure>

<h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><h3 id="安装出现报错：Unexpected-token-u0000-in-JSON-at-position-0"><a href="#安装出现报错：Unexpected-token-u0000-in-JSON-at-position-0" class="headerlink" title="安装出现报错：Unexpected token \u0000 in JSON at position 0"></a>安装出现报错：Unexpected token \u0000 in JSON at position 0</h3><p>执行命令清除错误缓存代码</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn cache clear</span><br></pre></td></tr></table></figure>
  </article>
  <aside class="table-content" id="site-toc">
  <div class="table-content-title">
    <i class="fa fa-arrow-right fa-lg" id="site-toc-hide-btn"></i>
    <span>目录</span>
  </div>
  <div class="table-content-main">
    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#yarn"><span class="toc-text">yarn</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="toc-text">常用命令</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#npm"><span class="toc-text">npm</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%8E%E7%BC%80%E7%AE%80%E4%BB%8B"><span class="toc-text">后缀简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4-1"><span class="toc-text">常用命令</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E5%B7%A5%E5%85%B7"><span class="toc-text">相关工具</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%95%9C%E5%83%8F%E6%BA%90%E7%AE%A1%E7%90%86"><span class="toc-text">镜像源管理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8C%85%E6%9B%B4%E6%96%B0%E7%AE%A1%E7%90%86"><span class="toc-text">包更新管理</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%97%AE%E9%A2%98"><span class="toc-text">问题</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85%E5%87%BA%E7%8E%B0%E6%8A%A5%E9%94%99%EF%BC%9AUnexpected-token-u0000-in-JSON-at-position-0"><span class="toc-text">安装出现报错：Unexpected token \u0000 in JSON at position 0</span></a></li></ol></li></ol></li></ol>
  </div>
</aside>

  
  
    <aside class="passage-copyright">
      <div>本文作者: 饿包子</div>
      
        <div>
          原文链接: 
          <a href="" target="_blank">https://lixianbin1.github.io/blog/2020/12/10/Yarn%E5%92%8Cnpm%E7%9A%84%E5%91%BD%E4%BB%A4%E9%9B%86%E5%92%8C%E9%97%AE%E9%A2%98/</a>
        </div>
      
      <div>
        版权声明: 本博客所有文章除特别声明外, 均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议. 转载请注明出处!
      </div>
    </aside>
  
  
    <div class="passage-tags">
     
      <a href="/blog/tags/Yarn/"><i class="fa fa-tags"></i>Yarn</a>
    
    </div>
  
</div>

    </main>
    
    
<div class="site-footer-wrapper">
<!--
  <div class="footer-top">
    <a class="left" href="/blog/2020/12/10/NPM%20%E7%9A%84%E5%91%BD%E4%BB%A4%E9%9B%86%E5%90%88%E5%8F%8A%E7%9B%B8%E5%85%B3%E7%9A%84%E9%97%AE%E9%A2%98/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a class="right" href="/blog/2020/12/09/%E3%80%90%E9%A1%B9%E7%9B%AE%E6%9E%84%E5%BB%BA%E3%80%91Vue%E9%A1%B9%E7%9B%AE%E7%9A%84%E5%BC%80%E5%A7%8B%E5%92%8C%E6%89%A9%E5%B1%95%E9%97%AE%E9%A2%98/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
  </div>
-->
  <footer class="site-footer">
    
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">网站推荐</h5>
          
            <span class="site-footer-item">
              <a href="https://tympanus.net/codrops/" target="_blank">Codrops</a>
            </span>
          
            <span class="site-footer-item">
              <a href="http://taobaofed.org/" target="_blank">淘宝前端团队(FED)</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">文档教程</h5>
          
            <span class="site-footer-item">
              <a href="https://es6.ruanyifeng.com/" target="_blank">ES6入门</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://developer.mozilla.org/zh-CN/docs/Web" target="_blank">MDN Web文档</a>
            </span>
          
        </div>
      
        <div class="site-footer-col">
          <h5 class="site-footer-title">兴趣前沿</h5>
          
            <span class="site-footer-item">
              <a href="https://threejs.org/" target="_blank">threejs</a>
            </span>
          
            <span class="site-footer-item">
              <a href="https://github.com/justadudewhohacks/face-api.js" target="_blank">face-api</a>
            </span>
          
        </div>
      
    
    <div class="site-footer-info">
      <i class="fa fa-clock-o"></i> 本站已稳定运行<span id="site-time"></span>
    </div>
    
      <div class="site-footer-info">
        <i class="fa fa-paw"></i> 您是本站第 <span id="site-count"></span> 位访客
      </div>
    
    
      <div class="site-footer-info">
        <i class="fa fa-at"></i> Email: xianbin.me@qq.com
      </div>
    
<!--     <div class="site-footer-info">
      <i class="fa fa-copyright"></i> 
      2019 <a href="https://github.com/dongyuanxin/theme-ad/" target="_blank">Theme-AD</a>.
      Created by <a href="https://godbmw.com/" target="_blank">GodBMW</a>.
      All rights reserved.
    </div> -->
  </footer>
</div>
    <div id="site-layer" style="display:none;">
  <div class="site-layer-content">
    <div class="site-layer-header">
      <span class="site-layer-header-title" id="site-layer-title"></span>
      <i class="fa fa-close" id="site-layer-close"></i>
    </div>
    <div class="site-layer-body" id="site-layer-container">
      <div class="site-layer-input" id="site-layer-search" style="display: none;">
        <input type="text">
        <i class="fa fa-search"></i>
      </div>
      
        <div class="site-layer-reward" id="site-layer-reward" style="display: none;">
          
            <div>
              <img src="/blog/images/wechat.png" alt="WeChat">
              
                <p>WeChat</p>
              
            </div>
          
            <div>
              <img src="/blog/images/alipay.png" alt="AliPay">
              
                <p>AliPay</p>
              
            </div>
          
        </div>
      
      <div id="site-layer-welcome" style="display:none;"></div>
    </div>
  </div>
</div>
    

<div class="bottom-bar">

  <div class="bottom-bar-left">
<!--
    <a href="/blog/2020/12/10/NPM%20%E7%9A%84%E5%91%BD%E4%BB%A4%E9%9B%86%E5%90%88%E5%8F%8A%E7%9B%B8%E5%85%B3%E7%9A%84%E9%97%AE%E9%A2%98/" data-enable="true">
      <i class="fa fa-arrow-left"></i>
    </a>
    <a href="/blog/2020/12/09/%E3%80%90%E9%A1%B9%E7%9B%AE%E6%9E%84%E5%BB%BA%E3%80%91Vue%E9%A1%B9%E7%9B%AE%E7%9A%84%E5%BC%80%E5%A7%8B%E5%92%8C%E6%89%A9%E5%B1%95%E9%97%AE%E9%A2%98/" data-enable="true">
      <i class="fa fa-arrow-right"></i>
    </a>
-->
  </div>

  
  
  
  <div class="bottom-bar-right">
    <a href="javascript:void(0);" data-enable="true" id="site-toc-show-btn">
      <i class="fa fa-bars"></i>
    </a>
    
      <a href="#site-comment" data-enable="true">
        <i class="fa fa-commenting"></i>
      </a>
    
    <a href="javascript:void(0);" id="site-toggle-share-btn">
      <i class="fa fa-share-alt"></i>
    </a>
    
    <a href="javascript:void(0);" id="back-top-btn">
      <i class="fa fa-chevron-up"></i>
    </a>
  </div>
</div>
    <div id="share-btn">
  
  
  
    <a id="share-btn-weibo" href="javascript:void(0);" target="_blank">
      <i class="fa fa-weibo"></i>
    </a>
  
  
    <a id="share-btn-qq" href="javascript:void(0);" target="_blank">
      <i class="fa fa-qq"></i>
    </a>
  
  
</div>
    





    
  </body>
</html>